package com.dudu.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import com.dudu.enumeration.AbilityTypeEnum;

public class VoiceUtil {

	public static final Logger log = core.util.Logger.getLogger(VoiceUtil.class.getName());
	
	/**
	 * 解析被叫参数
	 * @param calledJsonArray json数组[{"phone":"17001090122","msg_id":"49810560379194247739837403838602"}]
	 * @return
	 */
	public static String parsePhone(JSONArray calledJsonArray) {
		String returnData = "";
		try{
			StringBuffer calledBuffer = new StringBuffer();
			for(int i=0;i<calledJsonArray.size();i++){
				JSONObject calledJson = calledJsonArray.getJSONObject(i);
				String phone = calledJson.getString("phone");
				calledBuffer.append(phone);
				if(i+1 < calledJsonArray.size()){
					calledBuffer.append(",");
				}
			}
			returnData = calledBuffer.toString();
		}catch(Exception e){
			log.error("-----error---calledJsonArray解析错误：" + calledJsonArray.toString());
		}
		
		return returnData;
	}

	/**
	 * 根绝voiceType修改abilityType
	 * @param abilityType 	VOICE70：70字文字语音通知；VOICE140：140字文字语音通知；AUDEO70：70字录音语音通知；AUDEO140：40字录音语音通知
	 * @param voiceType 	VOICE70：70字语音通知；VOICE140：140字语音通知
	 * @param ttsOrAudio    是文字还是录音：tts，audio
	 * @return
	 */
	public static String changeAbilityTypeByVoiceType(String abilityType, String voiceType, String ttsOrAudio){
		if(AbilityTypeEnum.VOICE70.getKey().equals(voiceType) && "tts".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.VOICE70.getKey();
		}else if(AbilityTypeEnum.VOICE70.getKey().equals(voiceType) && "audio".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.AUDEO70.getKey();
		}else if(AbilityTypeEnum.VOICE140.getKey().equals(voiceType) && "tts".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.VOICE140.getKey();
		}else if(AbilityTypeEnum.VOICE140.getKey().equals(voiceType) && "audio".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.AUDEO140.getKey();
		}
		
		return abilityType;
	}
	
	/**
	 * 处理运营平台鉴权时的产品码
	 * @param abilityType
	 * @param voiceType
	 * @param ttsOrAudio
	 * @param type
	 * @return
	 */
	public static String changeAbilityTypeByVoiceType(String abilityType, String voiceType, String ttsOrAudio, String plat){
		if(AbilityTypeEnum.VOICE70.getKey().equals(voiceType) && "audio".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.VOICE70.getKey();
		}else if(AbilityTypeEnum.VOICE140.getKey().equals(voiceType) && "audio".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.VOICE140.getKey();
		}else if(AbilityTypeEnum.AUDEO70.getKey().equals(voiceType) && "audio".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.VOICE70.getKey();
		}else if(AbilityTypeEnum.AUDEO140.getKey().equals(voiceType) && "audio".equals(ttsOrAudio)){
			abilityType = AbilityTypeEnum.VOICE140.getKey();
		}
		
		return abilityType;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
